package main.remote;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client extends Thread {
	Socket socket;
	DataOutputStream out;
	DataInputStream in;

	public Client(String host, int port) throws IOException {
		socket = new Socket(host, port);
		out = new DataOutputStream(socket.getOutputStream());
		in = new DataInputStream(socket.getInputStream());
	}

	public Message call(Message message) {
		try {
			message.encode(out);
		} catch (Exception e) {
			System.err.println("MessageClient: Call (to) failure: " + e);
			return null;
		}

		try {
			Message m = new Message();
			m.decode(in);
			return m;
		} catch (Exception e) {
			System.err.println("MessageClient: Call (from) failure: " + e);
			return new Message();
		}
	}

	public void disconnect() {
		Message m = new Message();
		m.setType(0);
		m.setParam("$disconnect", "$disconnect");
		call(m);
		try {
			socket.close();
		} catch (Exception e) {
			System.err.println("ungraceful disconnect on client " + e);
		}
	}
}